{% extends 'rebs/layouts/base.html' %}

{% load static humanize widget_tweaks %}

{% block title %}Rebs - 계약 해지 관리{% endblock %}

{% block content %}

    <!-- Start Content-->
    <div class="container-fluid">

        <!-- start page title -->
        {% include 'rebs/partials/content_title.html' with  second_bs="분양 계약 관리" third_bs="계약 정보 관리" title="계약 해지 관리" %}
        <!-- end page title -->

        <div class="row">
            <form class="form-horizontal p-0 col-12" name="project_form" method="get">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body">

                            {% with menu_order='3' %}
                                {% include 'rebs_contract/partials/head_menu.html' %}
                            {% endwith %}

                            <div class="row">
                                {% include 'rebs/partials/project_select.html' %}
                                <div class="col-sm-12 col-md-6"></div>
                            </div>
                        </div>
                    </div>
                </div>


                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body pb-1">
                            <div class="row">
                                <div class="col-12 mb-3">
                                    <!-- Button Group -->
                                    <div class="btn-group mb-3">
                                        <button type="button" class="btn btn-light"
                                                {% if request.GET.contractor %}
                                                onclick="location.href='{% url 'rebs:contract:register' %}?project={{ contract.project_id }}&task=2&order_group={{ contract.order_group.id }}&type={{ contract.contractunit.unit_type.id }}&contract_unit={{ contract.contractunit.id }}&unit_number={{ contract.contractunit.unitnumber.id }}&cont_id={{ contract.id }}&p={{ request.GET.page }}'"
                                                {% else %}
                                                onclick="alert('계약자 정보를 선택하세요!')"
                                                {% endif %}>
                                            등록 계약 변경
                                        </button>
                                        <button type="button" class="btn btn-light" onclick="alert('준비중!')">주소(연락처) 변경
                                        </button>
                                        <button type="button" class="btn btn-light" onclick="alert('준비중!')">권리 의무 승계
                                        </button>
                                        <button type="button" class="btn btn-secondary"
                                                onclick="location.href='{% url 'rebs:contract:release' %}?project={{ this_project.id }}'">
                                            계약 해지 관리
                                        </button>
                                    </div>
                                </div>
                                <div class="form-group mb-0 justify-content-end row col-12">
                                    <div class="col-12">
                                        <div class="form-group col-12 row">
                                            <label for="id_task"
                                                   class="col-4 col-md-2 col-lg-1 col-form-label">구분</label>
                                            <div class="col-8 col-md-4 col-lg-2">
                                                <select name="task" id="id_task" class="form-control select2"
                                                        onchange="submit()">
                                                    {% if contractor.contractorrelease.status == '3' %}
                                                        <option value="5" {% if request.GET.task == '5' %}selected{% endif %}>해지 철회</option>
                                                    {% endif %}
                                                    <option value="" {% if request.GET.task == null %}selected{% endif %}>---------</option>
                                                    <option value="3" {% if request.GET.task == '3' %}selected{% endif %}>해지 신청</option>
                                                    <option value="4" {% if request.GET.task == '4' %}selected{% endif %}>해지 종결</option>
                                                </select>
                                            </div>

                                            <label for="id_contractor_sel"
                                                   class="col-4 col-md-2 col-lg-1 col-form-label">계약자 정보</label>
                                            <div class="col-8 col-md-4 col-lg-2">
                                                <select name="contractor" id="id_contractor_sel"
                                                        class="form-control select2" data-toggle="select2"
                                                        onchange="submit()">
                                                    {% if not request.GET.contractor %}
                                                        <option value="">---------</option>
                                                    {% endif %}
                                                    {% for ctor in contractors %}
                                                        <option value="{{ ctor.id }}">{{ ctor }}</option>
                                                    {% endfor %}
                                                </select>
                                            </div>
                                            <input type="hidden" name="release_id" value="{{ request.GET.release_id }}">
                                        </div>
                                        {% if request.GET.task == '1' %}
                                            <span class="text-danger">해지신청 철회(취소) 처리하시겠습니까?</span>
                                        {% endif %}
                                        {% if request.GET.task == '4' %}
                                            <span class="text-danger">해지 종결 처리 후 해당 데이터를 계약상태로 되돌릴 수 없습니다. 반드시 환불 완료된 경우에만 진행하십시오!</span>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>

            <form method="post" name="form1" id="form1" class="col-12 form-horizontal needs-validation p-0" novalidate>
                {% csrf_token %}
                {# hidden field zone #}
                {{ form.project }}{{ form.project.errors }}
                {{ form.contractor }}{{ form.contractor.errors }}
                {{ form.status }}{{ form.status.errors }}
                {{ form.register }}{{ form.register.errors }}
                {# hidden field zone #}
                {{ form.errors }}
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="row">
                                <div class="form-group row mb-3 col-12 col-md-6">
                                    <label for="{{ form.refund_amount.id_for_label }}"
                                           class="col-3 col-form-label">{{ form.refund_amount.label }}</label>
                                    <div class="col-9">
                                        {{ form.refund_amount|add_class:"form-control" }}
                                    </div>
                                </div>

                                <div class="form-group row mb-3 col-12 col-md-6">
                                    <label for="{{ form.refund_account_bank.id_for_label }}"
                                           class="col-3 col-form-label">{{ form.refund_account_bank.label }}</label>
                                    <div class="col-9">
                                        {{ form.refund_account_bank|add_class:"form-control" }}
                                    </div>
                                </div>

                                <div class="form-group row mb-3 col-12 col-md-6">
                                    <label for="{{ form.refund_account_number.id_for_label }}"
                                           class="col-3 col-form-label">{{ form.refund_account_number.label }}</label>
                                    <div class="col-9">
                                        {{ form.refund_account_number|add_class:"form-control" }}
                                    </div>
                                </div>

                                <div class="form-group row mb-3 col-12 col-md-6">
                                    <label for="{{ form.refund_account_depositor.id_for_label }}"
                                           class="col-3 col-form-label">{{ form.refund_account_depositor.label }}</label>
                                    <div class="col-9">
                                        {{ form.refund_account_depositor|add_class:"form-control" }}
                                    </div>
                                </div>

                                <div class="form-group row mb-3 col-12 col-md-6">
                                    <label for="{{ form.request_date.id_for_label }}"
                                           class="col-3 col-form-label">{{ form.request_date.label }}</label>
                                    <div class="col-9">
                                        {{ form.request_date|add_class:"form-control"|attr:"placeholder:해지신청일"|attr:"data-provide=datepicker data-date-format=yyyy-mm-dd data-date-autoclose=true data-toggle=input-mask data-mask-format=0000-00-00" }}
                                    </div>
                                </div>

                                <div class="form-group row mb-3 col-12 col-md-6">
                                    <label for="{{ form.completion_date.id_for_label }}"
                                           class="col-3 col-form-label">{{ form.completion_date.label }}</label>
                                    <div class="col-9">
                                        {{ form.completion_date|add_class:"form-control"|attr:"placeholder:해지종결일"|attr:"data-provide=datepicker data-date-format=yyyy-mm-dd data-date-autoclose=true data-toggle=input-mask data-mask-format=0000-00-00" }}
                                    </div>
                                </div>

                                <div class="form-group row mb-3 col-12 col-md-6">
                                    <label for="{{ form.note.id_for_label }}"
                                           class="col-3 col-form-label">{{ form.note.label }}</label>
                                    <div class="col-9">
                                        {{ form.note|add_class:"form-control"|attr:"placeholder:비고"|attr:"rows:3" }}
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-body">
                            <div class="row">
                                <div class="form-group mb-0 justify-content-end row">
                                    <div class="col-12">
                                        {% if request.user.is_superuser or request.user.staffauth.contract > '1' %}
                                            <button type="submit" class="btn btn-{% if request.GET.release_id %}success{% else %}primary{% endif %}">등록하기</button>
                                        {% else %}
                                            <button type="button" class="btn btn-outline-secondary" onclick="alert('계약 등록 관리 권한이 없습니다. 관리자에게 문의하여 주십시요.')">조회권한 사용자</button>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>

            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        {% include 'rebs_contract/partials/release_table.html' %}
                    </div>

                    {% if is_paginated %}

                        {# to_first_last :: 맨처음 / 맨끝으로 버튼 사용 여부 #}
                        {# page_class ex:: pagination-rounded pagination-sm pagination-lg justify-content-center justify-content-end #}
                        {% include 'partials/pagination.html' with to_first_last="ok" page_class="pagination-rounded justify-content-end" %}

                    {% endif %}
                </div>
            </div>
        </div>
    </div> <!-- container -->

{% endblock %}

{% block third_party_js %}

    <script>
      const project = document.getElementById('id_project_sel')
      const contractor = document.getElementById('id_contractor_sel')

      window.onload = function () {
        project.setAttribute('onchange', 'projectSelect()')
        project.value = {{ this_project.id }}
        {% if request.GET.contractor %}
          contractor.value = {{ request.GET.contractor }}
        {% endif %}
        {% if request.GET.task == '4' %}
          document.getElementById('id_refund_amount').required = true
          document.getElementById('id_refund_account_bank').required = true
          document.getElementById('id_refund_account_number').required = true
          document.getElementById('id_refund_account_depositor').required = true
          document.getElementById('id_completion_date').required = true
        {% endif %}
      }

      function projectSelect() {
        document.project_form.submit()
      }
    </script>

{% endblock %}
